
package server;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Wrapper class for encapsulating HTTP response codes
 * (In future could be extended to containt significiant
 *  header line flasg for particular HTTP code)
 * @author Peewhy
 */
class HttpError {
    String title;
    
    public HttpError(String t){
        title = t;
    }
}


/**
 * Class for handling HTTP respond (Error) codes and String messages.
 * @author Peewhy
 */
public class ErrorCode {
    private static Map<Integer, HttpError> map;
    static {
        map = new HashMap<Integer, HttpError> ();
        
        // OK codes
        map.put(200, new HttpError("OK"));
        map.put(201, new HttpError("Created"));
        map.put(202, new HttpError("Accepted"));
        map.put(204, new HttpError("No content"));
        
        // Redirection        
        map.put(301, new HttpError("Multiple choices"));
        map.put(302, new HttpError("Moved permanently"));
        map.put(303, new HttpError("Moved temporarly"));
        map.put(304, new HttpError("Not modified"));
        
        // Client error
        map.put(400, new HttpError("Illegal request"));
        map.put(401, new HttpError("Unauthorized"));
        map.put(403, new HttpError("Forbidden"));
        map.put(404, new HttpError("Not found"));
        
        // Server error
        map.put(500, new HttpError("Internal Server Error"));
        map.put(501, new HttpError("Not implemented"));
        map.put(502, new HttpError("Service Unavailable"));        
    }
    
    // Prevent from creating instances
    private ErrorCode() {
        
    }
    /**
     * Returns string representation of HTTP response code
     * @param code HTTP response code
     * @return string representation
     * @throws java.lang.IllegalArgumentException if status code not defined
     */
    public static String getString(int code) throws IllegalArgumentException {
        if (map.containsKey(code)) {
            return map.get(code).title;
        }
        else {
            Logger.getLogger(Server.LOG).log(Level.SEVERE, "Unknown HTTP response code " + code);
            throw new IllegalArgumentException("Unrecognized status code: " + code);
        }
    }

}
